#include "StdAfx.h"
#include "DataManager.h"
#include <algorithm>
using namespace std;

CDataManager g_objDataManager;


CDataManager::CDataManager(void)
{
}


CDataManager::~CDataManager(void)
{
	this->Clear();
}

void CDataManager::Clear()
{
	for (int i=0; i<(int) m_setData.size(); i++)
	{
		SAFE_DELETE(m_setData[i]->data[0]);
		SAFE_DELETE(m_setData[i]->data[1]);
		SAFE_DELETE(m_setData[i]);

	}
	m_setData.clear();
	m_mapData.clear();
}

bool CDataManager::Init()
{
	this->Clear();
	if (!this->LoadData("data_o.ini", DATA_BIGBOARD))
	{
		return false;
	}

	if (!this->LoadData("data.ini", DATA_ONESTOCKS))
	{
		return false;
	}
	
	sort(m_setData.begin(), m_setData.end(), compareByTime);
	
	this->CalcMoveAverageLine(5, EAV_5 );
	this->CalcMoveAverageLine(10, EAV_10);
	this->CalcMoveAverageLine(20, EAV_20);
	this->CalcMoveAverageLine(23, EAV_23);
	this->CalcMoveAverageLine(30, EAV_30);
	this->CalcMoveAverageLine(50, EAV_50);
	this->CalcMoveAverageLine(55, EAV_55);
	this->CalcMoveAverageLine(60, EAV_60);

	return true;
}								 

bool CDataManager::compareByTime(DAY_PERDATA* rl, DAY_PERDATA* rh)
{
	return rl->date.Year*10000+rl->date.Month*100 + rl->date.Day < rh->date.Year*10000+rh->date.Month*100 + rh->date.Day;
}

bool CDataManager::LoadData(const char* pszFile, int nDataType)
{
	FILE* pFile = fopen(pszFile, "rb+");
	if (!pFile)
	{
		return false;
	}

	PER_DATA* pPreData = NULL;
	char szLine[1024]	= "";
	while(true)
	{
		if (!fgets(szLine, sizeof(szLine), pFile)){
			break;
		}

		int nYear =0,nMonth =0,nDay =0;
		float fBegin =0,fHi =0,fLo =0,fEnd =0,fAmount= 0,fMoney =0;

		if (sscanf(szLine, "%d-%d-%d %f %f %f %f %f %f", &nYear,&nMonth,&nDay,&fBegin,&fHi,&fLo,&fEnd,&fAmount,&fMoney)!= 9){
			continue;
		}

		PER_DATA* pData = new PER_DATA;
		memset(pData, 0, sizeof(PER_DATA));
		
		pData->Begin = fBegin;
		pData->End = fEnd;
		pData->Hi = fHi;
		pData->Lo = fLo;
		pData->Amount = fAmount;
		pData->Money = fMoney;
		pData->preData = pPreData;
		pPreData = pData;
		int nDate = nYear*10000 + nMonth*100 + nDay;
		if (m_nStocksBeginTime ==0 && nDataType == DATA_ONESTOCKS){
			m_nStocksBeginTime = nDate;
		}

		if (m_mapData[nDate] == NULL){
			m_mapData[nDate] = new DAY_PERDATA();
			m_mapData[nDate]->date.Year = nYear;
			m_mapData[nDate]->date.Month= nMonth;
			m_mapData[nDate]->date.Day  = nDay;
			m_setData.push_back(m_mapData[nDate]);
		}
		m_mapData[nDate]->data[nDataType] = pData;		
	}

	fclose(pFile);
	return true;
}

void CDataManager::CalcMoveAverageLine(int nDay, int nType)
{
	int nSort = DATA_SIZE;
	while (--nSort >=0)
	{
		PER_DATA* pData = NULL;
		for (int i= (int)m_setData.size()-1; i>=0; i--){
			DAY_PERDATA* pDayData = m_setData[i];
			if (pDayData->data[nSort] != NULL){
				pData = pDayData->data[nSort];
				break;
			}
		}

		while (pData)
		{
			int nCount = nDay;
			PER_DATA* pCurData = pData;
			float fTotal = 0;
			while (nCount-- >0 && pCurData)
			{
				fTotal += pCurData->End;
				pCurData = pCurData->preData;
			}

			if (nCount < 0){
				pData->AvData[nType] = fTotal/nDay;	
			}
			else{
				break;
			}
			pData = pData->preData;
		}
	}
}

DAY_PERDATA* CDataManager::GetData(int nIndex)
{
	if (nIndex <0 || nIndex >= m_setData.size())
	{
		return NULL;
	}
	
	return m_setData[nIndex];
}